#!/usr/bin/env python
#NMre
"""NM re Extentions
"""
import re
     
def reStripParenthesis(str,count = 0):
    return reReplStrBetween(str,'\(','\)','',count) 
def reSafe(restr):                                           #very simplisic
    restr.replace(' ','\\s').replace('$','\\$')
def reAdd(regexGroupExp,strToSearch,prepend='',append='',count=0):                  
    return re.sub(regexGroupExp, prepend + '\\g<0>' + append ,strToSearch ,count)
def reReplace(rawstr,replaceStr,strToSearch,count = 0):
    return re.sub(rawstr, replaceStr, strToSearch,count)
def reFindReplace(rawstr,replaceStr,strToSearch,count = 0):
    r0=re.findall(rawstr ,strToSearch)
    r1=re.subn(rawstr, replaceStr, strToSearch,count)
    return(r0,r1[0],r1[1])                                              #([found strings],String after replacement,howMany Substituted)

#def reGetMatch(resstr,str,group=[1]):
#    rt=[]
#    match_obj = re.search(resstr,str)   
#    return match_obj
#    if  match_obj:
#        for el in group:
#            if isinstance(el,ont):rt.append(match_obj.groups()[el])
#            else:                 rt.append(match_obj.group(el)) 
#    return rt   
def reReplStrBetween(str,startStr,endStr,replaceStr='',count = 0):
    rawstr = startStr + ".*" + endStr
    return re.sub(rawstr, replaceStr, str,count)

def reReplStrBetweenV2(str,startStr,endStr,replaceStr='',count = 0):
    rawstr = startStr + ".*?" + endStr 
    compile_obj = re.compile(rawstr,  re.MULTILINE| re.DOTALL| re.VERBOSE)
    return compile_obj.sub(replaceStr, str, count)  
def reSlugify(s):
    return re.sub('[^a-zA-Z0-9-]+', '-', s).strip('-')
